<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CIBlog
 *
 * 使用codeigniter框架开发的一个博客。
 * 
 * @package     ciblog
 * @author      Eric
 * @copyright   Copyright (c) 2011 - 2012, Eric
 * @license     GNU General Public License 2.0
 * @link        http://www.hebaijun.com
 * @version     1.0
 */

 //----------------------------------------------------------

 /**
  * 取留言信息的模型
  *
  * 
  *
  * @package     ciblog
  * @subpackage  Models
  * @category    Models
  * @author      Eric <baijun.he@163.com>
  * @link        http://www.hebaijun.com
  */
class Comment_m extends CI_Model {

	/**
     * 构造函数
     * 
     * @access public
     * @return void
     */
    public function __construct()
    {
		parent::__construct();
		
		log_message('debug', "CIBlog: Comment Model Class Initialized");

		//加载缓存适配器，如果memcached不支持 则用file类型缓存
		$this->load->driver('cache', array('adapter' => config_item('cache_adapter'), 'backup' => config_item('cache_backup')));

		$this->table_comment = $this->db->dbprefix('comments');
    }

	/**
     * 取得留言信息
     * 
     * @access public
	 * @param string 需要取得的字段
	 * @param array $search 查询条件
     * @return array 所有自动加载的系统设置
     */
    public function load_comments($param = '*' ,$search = array())
    {
		//返回值
		$comments = FALSE;

		$this->db->select($param);

		if( ! empty( $search ) )
		{
			//通过文章id取评论
			if(isset($search['comment_post_id']) && ! empty($search['comment_post_id']))
			{
				$this->db->where('comment_post_id', $search['comment_post_id']);
			}
			
			//通过IP取评论
			if(isset($search['comment_author_IP']) && ! empty($search['comment_author_IP']))
			{
				$this->db->where('comment_author_IP', $search['comment_author_IP']);
			}

			//评论状态
			if(isset($search['comment_approved']))
			{
				$this->db->where('comment_approved', $search['comment_approved']);
			}

			if(isset($search['limit']) && ! empty($search['limit']))
			{
				if(isset($search['offset']) && ! empty($search['offset']))
				{
					$this->db->limit($search['limit'], $search['offset']);
				}
				else
				{
					$this->db->limit($search['limit'], 0);
				}	
			}

			if(isset($search['orderby']) && ! empty($search['orderby']))
			{
				if(isset($search['order']) && ! empty($search['order']))
				{
					$this->db->order_by($search['orderby'], $search['order']);
				}
				else
				{
					$this->db->order_by($search['orderby']);
				}	
			}
		}

		$this->db->from($this->table_comment);
		$sql = $this->db->_compile_select();

		$key = md5('comment_'.$sql);
		$comments = $this->cache->get($key);
		if($comments === FALSE)
		{
			$query = $this->db->get();
			$comments = $query->result();
			$this->cache->save($key,$comments,config_item('cache_expire'));
		}

		//清空查询条件
		$this->db->_reset_select();

		return $comments;
    }

	/**
     * 添加留言信息
     * 
     * @access public
	 * @param array $comment 评论数据
     * @return string 返回插入后的id 或者FALSE
     */
	function add_comment( $comment = array())
	{
		$this->db->insert($this->table_comment, $comment);
		if($this->db->affected_rows() === 1)
		{
			$this->cache->clean();
			return $this->db->insert_id();
		}
		else
		{
			return FALSE;
		}
	}

	/**
     * 获取评论的文章信息
     * 
     * @access public
	 * @param string 需要取得的字段
	 * @param array $search 查询条件
     * @return array 所有自动加载的系统设置
     */
	function get_post_comment( $param = '*' ,$search = array() )
	{
		//返回值
		$posts = FALSE;

		$table_posts = $this->db->dbprefix('posts');

		$this->db->select($param);

		if( ! empty( $search ) )
		{
			if(isset($search['post_id']) && ! empty($search['post_id']))
			{
				$this->db->where('post_id', $search['post_id']);
			}

			if(isset($search['limit']) && ! empty($search['limit']))
			{
				if(isset($search['offset']) && ! empty($search['offset']))
				{
					$this->db->limit($search['limit'], $search['offset']);
				}
				else
				{
					$this->db->limit($search['limit'], 0);
				}	
			}

			if(isset($search['orderby']) && ! empty($search['orderby']))
			{
				if(isset($search['order']) && ! empty($search['order']))
				{
					$this->db->order_by($search['orderby'], $search['order']);
				}
				else
				{
					$this->db->order_by($search['orderby']);
				}	
			}
		}

		$this->db->from($table_posts);
		
		if($posts === FALSE)
		{
			$query = $this->db->get();
			$posts = $query->result();
		}

		//清空查询条件
		$this->db->_reset_select();

		return $posts;
	}

	
}

/* End of file comment_m.php */
/* Location: ./application/models/comment_m.php */